package com.fy.spring.controller;


import com.fy.spring.entities.CommonResult;
import com.fy.spring.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = "/consumer/payment")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    //    public static final String PAYMENT_URL = "http://localhost:8001";  单机版
   /*
    集群版，一旦使用eureka服务名调用，
    那么必须在RestTemplate注入的时候添加@LoadBalanced，
    否则不管被调用的服务是否是集群，都报错找不到UnknownHostException
    */
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @PostMapping("/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }
}
